/****************************************************
* Unity版本：2022.1.21f1c1
* Version：0.1
* Company：DefaultCompany
* 日期：2023/06/21 14:29:23
*****************************************************/

/****************************************************
* Unity版本：2021.3.14f1c1
* Version：0.1
* Company：HuaYi
* 日期：2023/06/21 14:24:07
*****************************************************/

using System;
/// <summary>
///PipeLine模式的几个角色
///阀门 处理数据的节点
///管道 组织各个阀门
///客户端 构造管道，并调用
/// </summary>
namespace PileLineSystem
{
    //阀门接口
    public interface IValve
    {
        IValve GetNext();
        void SetNext(IValve v);
        void Invoke(string s);
    }
    //管道接口
    public interface IPipeLine
    {
        IValve GetFirst();
        IValve GetBasic();
        void SetBasic(IValve v);
        void AddValve(IValve v);
    }
    /// <summary>
    ///尾阀门
    /// </summary>
    public class BasicValve : IValve
    {
        private IValve _nextValve;
        public IValve GetNext()
        {
            return _nextValve;
        }
        public void SetNext(IValve v)
        {
            _nextValve = v;
        }
        public void Invoke(string s)
        {
            string org = s;
            Console.WriteLine($"Basic valve invoked! Will replace a with b");
            s = s.Replace('a', 'b');
            Console.WriteLine($"Basic valved handled: {org} => {s}");
        }
    }
    /// <summary>
    /// 普通阀门
    /// </summary>
    /// 
    public class FirstValve : IValve
    {
        private IValve _nextValve;
        public IValve GetNext()
        {
            return _nextValve;
        }
        public void SetNext(IValve v)
        {
            _nextValve = v;
        }
        public void Invoke(string s)
        {
            string org = s;
            Console.WriteLine($"First valve invoked! Will replace 1 with 2");
            s = s.Replace('1', '2');
            Console.WriteLine($"First valved handled: {org} => {s}");

            GetNext()?.Invoke(s);//将数据传递到下一个阀门
        }
    }
    public class SecondValve : IValve
    {
        private IValve _nextValve;
        public IValve GetNext()
        {
            return _nextValve;
        }
        public void SetNext(IValve v)
        {
            _nextValve = v;
        }
        public void Invoke(string s)
        {
            string org = s;
            Console.WriteLine($"Second valve invoked! Will replace 5 with 6");
            s = s.Replace('5', '6');
            Console.WriteLine($"Second valved handled: {org} => {s}");

            GetNext()?.Invoke(s);//将数据传递到下一个阀门
        }
    }

    public class StandardPipeLine : IPipeLine
    {

        private IValve _firstValve;
        private IValve _basicValve;
        public IValve GetFirst()
        {
            return _firstValve;
        }
        public IValve GetBasic()
        {
            return _basicValve;
        }
        public void SetBasic(IValve v)
        {
            _basicValve = v;
        }
        public void AddValve(IValve v)
        {
            if (_firstValve == null)
            {
                _firstValve = v;
                v.SetNext(_basicValve);
            }
            else
            {
                IValve current = _firstValve;
                while (current != null)
                {
                    if (current.GetNext() == _basicValve)
                    {
                        current.SetNext(v);
                        v.SetNext(_basicValve);
                        break;
                    }
                    current = current.GetNext();
                }
            }
        }
    }

}